package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysAqiStandardDayInfo;
import com.emep.changzhi.analyse.repository.basics.SysAqiStandardDayRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: aqi达标、污染天数业务层
 * @date 2018/3/23 11:06
 * @version V1.0
 */

@Slf4j
@Data
@Service
@Transactional
public class SysAqiStandardDayService {
    @Resource
    private SysAqiStandardDayRepository sysAqiStandardDayRepository;

    /**
     *
     * 新增污染、正常天气
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysAqiStandardDayInfo save(Map<String, Object> payload) {
        SysAqiStandardDayInfo sysAqiStandardDayInfo = null;
        try {
            SysAqiStandardDayInfo sysAqiStandardDayInfo1 = (SysAqiStandardDayInfo) MapUtils.convertMap(SysAqiStandardDayInfo.class,payload);
            sysAqiStandardDayInfo = sysAqiStandardDayRepository.save(sysAqiStandardDayInfo1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysAqiStandardDayInfo;
    }
    /**
     *查询今年所有信息
     *
     * @return
     */
    public List<SysAqiStandardDayInfo> list() {
        List<SysAqiStandardDayInfo> sysAqiStandardDayInfos = null;
        try {
            String year=new SimpleDateFormat("yyyy").format(new Date());
            sysAqiStandardDayInfos =  sysAqiStandardDayRepository.findAllByYear(year);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysAqiStandardDayInfos;
    }

    /**
     * @author xy
     * @Description: doto
     * @date 2018/3/30 11:07
     * @version V1.0
     */
    public List<SysAqiStandardDayInfo> listAll() {
        List<SysAqiStandardDayInfo> sysAqiStandardDayInfos = null;
        try {
            sysAqiStandardDayInfos =  sysAqiStandardDayRepository.findAll();
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysAqiStandardDayInfos;
    }
}
